//
//  customPresent.m
//  testTransition3
//
//  Created by zsj1992 on 17/1/4.
//  Copyright © 2017年 bjhj. All rights reserved.
//

#import "customPresent.h"

#import "SecondViewController.h"
#define SCREEN_W [UIScreen mainScreen].bounds.size.width
#define SCREEN_H [UIScreen mainScreen].bounds.size.height



@interface customPresent()
@property (nonatomic,strong)id transitionContext;


@end

@implementation customPresent

//返回自定义动画的时间长度
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{

    return 0.8;
}




//返回自定义动画的动画方式

-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{

    
//    //获取toVc
//    SecondViewController * toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
//    
//    
//    //获取容器View
//    UIView * containerView = [transitionContext containerView];
//    
//    
//    [containerView addSubview:toVc.view];
//    
//    //获取到控制器的最终位置
//    CGRect finalFrame = [transitionContext finalFrameForViewController:toVc];
//    //最终位置向下
//    toVc.view.frame = CGRectOffset(finalFrame, 0, SCREEN_H);
//    
//    
//    //添加一个弹性的动画
//    [UIView animateWithDuration:0.7 delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
//        
//        toVc.view.frame = finalFrame;
//
//    } completion:^(BOOL finished) {
//        
//        [transitionContext completeTransition:YES];
//
//    }];

    
    
    
    
    
    
    
    self.transitionContext = transitionContext;
    
    
    //获取toVc
    SecondViewController * toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    
    
    //获取容器View
    UIView * containerView = [transitionContext containerView];
    
    
    [containerView addSubview:toVc.view];
    
    //获取到控制器的最终位置
    CGRect finalFrame = [transitionContext finalFrameForViewController:toVc];
    //最终位置向下
    toVc.view.frame = CGRectOffset(finalFrame, 0, SCREEN_H);
    
    NSTimeInterval duration = [self transitionDuration:transitionContext];

    //添加一个弹性的动画
    [UIView animateWithDuration:duration delay:0 usingSpringWithDamping:0.6 initialSpringVelocity:0 options:UIViewAnimationOptionCurveLinear animations:^{
        
        toVc.view.frame = CGRectOffset(toVc.view.frame, 0, -SCREEN_H);
        
        
    } completion:^(BOOL finished) {
        NSLog(@"complete");
        [self.transitionContext completeTransition:YES];
        
    }];
    

}









@end
